#!/bin/bash

# 设置颜色输出
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'

# 查找运行在8116端口的进程
pid=$(lsof -t -i:8116)

if [ ! -z "$pid" ]; then
    echo -e "${YELLOW}Stopping FastAPI server (PID: $pid)...${NC}"
    kill -9 $pid
    
    # 等待端口释放
    echo -e "${YELLOW}Waiting for port 8116 to be released...${NC}"
    for i in {1..10}; do
        if ! lsof -i:8116 >/dev/null 2>&1; then
            echo -e "${GREEN}Server stopped successfully${NC}"
            # 额外等待1秒确保端口完全释放
            sleep 1
            exit 0
        fi
        sleep 1
    done
    echo -e "${RED}Failed to stop server after 10 seconds${NC}"
    exit 1
else
    echo -e "${GREEN}No FastAPI server running on port 8116${NC}"
fi 